package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.Footprint;
import com.abaaba.project2.bean.FootprintExample;
import com.abaaba.project2.bean.Goods;
import com.abaaba.project2.bean.bo.WxGetValueIdBo;
import com.abaaba.project2.bean.vo.WxFootprintSingleVo;
import com.abaaba.project2.bean.vo.WxFootprintVo;
import com.abaaba.project2.mapper.FootprintMapper;
import com.abaaba.project2.mapper.GoodsMapper;
import com.abaaba.project2.service.WxFootprintService;
import com.abaaba.project2.util.UserIdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WxFootprintServiceImpl implements WxFootprintService {
    @Autowired
    FootprintMapper footprintMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public WxFootprintVo getFootprintList(Integer userId, Integer page, Integer size) {
        PageHelper.startPage(page, size);

        FootprintExample footprintExample = new FootprintExample();
        footprintExample.setOrderByClause("add_Time" + " " + "desc");
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        criteria.andUserIdEqualTo(userId).andDeletedEqualTo(false);

        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        List<WxFootprintSingleVo> footprintSingleVos = new ArrayList<>();
        for (Footprint footprint : footprints) {
            Goods goods = goodsMapper.selectByPrimaryKey(footprint.getGoodsId());
            footprintSingleVos.add(new WxFootprintSingleVo(goods.getBrief(), goods.getPicUrl(),
                    footprint.getAddTime(), goods.getId(), goods.getName(),
                    footprint.getId(), goods.getRetailPrice()));
        }

        // 查询的是哪个，就填入哪个；如这里不能填 footprintSingleVos
        PageInfo<Footprint> wxFootprintSingleVoPageInfo = new PageInfo<>(footprints);
        int pages = wxFootprintSingleVoPageInfo.getPages();
        WxFootprintVo wxFootprintVo = new WxFootprintVo();
        wxFootprintVo.setFootprintList(footprintSingleVos);
        wxFootprintVo.setTotalPages(pages);
        return wxFootprintVo;
    }

    @Override
    public Integer deleteFootprintList(Integer id) {
        Footprint footprint = footprintMapper.selectByPrimaryKey(id);
        footprint.setDeleted(true);

        return footprintMapper.updateByPrimaryKey(footprint);
    }

    @Override
    public void addFootprint(Integer id) {
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        Integer userId = UserIdUtil.getUserId();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Footprint footprint = new Footprint(null, userId, goods.getId(), new Date(),
                new Date(), false);
        footprintMapper.insertSelective(footprint);
    }
}
